package Entities;

import digitshoplogic.DBAccessSingleton;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Discount {
	public int UPC;
	public int ruleId;
	public Date from_Date;
	public Date to_Date;

    public Discount() {
    }

    public Discount(int UPC, int ruleId, Date from_Date, Date to_Date) {
        this.UPC = UPC;
        this.ruleId = ruleId;
        this.from_Date = from_Date;
        this.to_Date = to_Date;
    }
   // all attributes are part of Pre key, get function is not needed
  /*  public static final Discount getDiscountByKey(int did) throws SQLException {
        Discount dis = new Discount();

        PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("select * from_Date discount where discount.classId = ?");
         ps.setInt(1, did);

        ResultSet rs = ps.executeQuery();

         if (rs.next()) {
            dis.UPC = rs.getInt(1);
            dis.ruleId = rs.getInt(2);
            dis.from_Date = rs.getDate(3);
            dis.to = rs.getDate(4);
        }
        rs.close();
        con.close();
        return dis;
        }*/

     public static final void addDiscountByObj(Discount dis) throws SQLException {

         PreparedStatement ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("INSERT INTO discount VALUES (?,?,?,? )");

         ps.setInt(1, dis.UPC);
         ps.setInt(2, dis.ruleId);
         ps.setDate(3, dis.from_Date);
         ps.setDate(4, dis.to_Date);

         ps.executeUpdate();
         ps.close();
         con.close();
        }

 public static final void deleteDiscountByObj(Discount dis) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("DELETE FROM discount "
                 + " WHERE discount.UPC= ? AND "
                       + "discount.ruleId = ? AND "
                       + "discount.from_Date = ? AND "
                       + "discount.to_Date = ? ");

         ps.setInt(1, dis.UPC);
         ps.setInt(2, dis.ruleId);
         ps.setDate(3, dis.from_Date);
         ps.setDate(4, dis.to_Date);

         ps.executeUpdate();
         ps.close();
         con.close();
     }

    public int getUPC() {
        return UPC;
    }

    public void setUPC(int UPC) {
        this.UPC = UPC;
    }

    public Date getFrom() {
        return from_Date;
    }

    public void setFrom(Date from_Date) {
        this.from_Date = from_Date;
    }

    public int getRuleId() {
        return ruleId;
    }

    public void setRuleId(int ruleId) {
        this.ruleId = ruleId;
    }

    public Date getTo() {
        return to_Date;
    }

    public void setTo(Date to_Date) {
        this.to_Date = to_Date;
    }

}